﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "GbmpCopyActionBehavior.h"
#include "IUiDocument.h"
#include "IDocument.h"
#include "IElement.h"
#include "IElementCopyPasteComponent.h"
#include "IUiView.h"
#include "IUserTransaction.h"
#include "ICopyOperator.h"
#include "GbmpMoveUtils.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

GbmpCopyActionBehavior::GbmpCopyActionBehavior()
{

}

void GbmpCopyActionBehavior::OnLineFinished(IUiView* pCurrentView, const gcmp::Vector3d& startPt, const gcmp::Vector3d& endPt, const std::vector<ElementId>& elementIdsToBeCopied) const
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pCurrentView, L"pCurrentView为空",L"GDMPLab",L"2024-03-30");
    IUiDocument* pUiDoc = pCurrentView->GetUiDocument();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pUiDoc, L"pUiDoc为空",L"GDMPLab",L"2024-03-30");
    IDocument* pDoc = pUiDoc->GetDbDocument();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pDoc, L"doc为空",L"GDMPLab",L"2024-03-30");

    OwnerPtr<IUserTransaction> ut = IUserTransaction::Create(pDoc, GBMP_TR(L"复制"));
    DBG_WARN_AND_RETURN_VOID_UNLESS(ut != nullptr, L"事务创建不成功",L"GDMPLab",L"2024-03-30");

    const ICopyOperator* pDefaultCopyOperator = ICopyOperator::Get();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pDefaultCopyOperator != nullptr, L"默认的拷贝器不应该为空",L"GDMPLab",L"2024-03-30");

    const std::vector<ElementId>& toMoveModelIds = pDefaultCopyOperator->CopyElements(pDoc, elementIdsToBeCopied, std::map<ElementId, ElementId>(), nullptr);

    const Vector3d& moveDir = endPt - startPt;
    GbmpMoveUtils::MoveElements(pDoc, toMoveModelIds, startPt, moveDir, pCurrentView->GetModelView());

    ut->Commit();
}

std::vector<ElementId> GbmpCopyActionBehavior::GetElementIdsToBeCopied(IUiView* pCurrentView, const std::vector<ElementId>& selectedElementIds) const
{
    std::vector<ElementId> elementIdsToBeCopied;
    DBG_WARN_AND_RETURN_UNLESS(pCurrentView, elementIdsToBeCopied, L"pCurrentView为空",L"GDMPLab",L"2024-03-30");
    IUiDocument* pUiDoc = pCurrentView->GetUiDocument();
    DBG_WARN_AND_RETURN_UNLESS(pUiDoc, elementIdsToBeCopied, L"pUiDoc为空",L"GDMPLab",L"2024-03-30");
    IDocument* pDoc = pUiDoc->GetDbDocument();
    DBG_WARN_AND_RETURN_UNLESS(pDoc, elementIdsToBeCopied, L"doc为空",L"GDMPLab",L"2024-03-30");

    FOR_EACH(item, selectedElementIds)
    {
        if (const IElement * pCurEle = pDoc->GetElement(item))
        {
            const IElementCopyPasteComponent* pICopyPasteComponent = pCurEle->GetCopyPasteComponent();
            GBMP_ASSERT(pICopyPasteComponent != nullptr);
            std::wstring cannotCopiedExplanation;
            if (pICopyPasteComponent->CanBeCopied(&cannotCopiedExplanation))
            {
                elementIdsToBeCopied.push_back(item);
            }
        }
    }
    return elementIdsToBeCopied;
}

const std::wstring GbmpCopyActionBehavior::GetPromptMessage(CopyActionStatus drawLineStatus) const
{
    switch (drawLineStatus)
    {
    case CopyActionStatus::GettingStartPoint:
        return GBMP_TR(L"拷贝并移动选择的物体 - 请输入起始点");
    case CopyActionStatus::GettingEndPoint:
        return GBMP_TR(L"拷贝并移动选择的物体 - 请输入终止点");
    default:
        return L"";
    }
}
